package com.fenghuajueli.libbasecoreui.manager;

import com.fenghuajueli.lib_data.entity.db.DayPlanDbEntity;
import com.fenghuajueli.lib_data.entity.db.DayPlanDbEntityDao;
import com.fenghuajueli.lib_data.entity.db.MonthPlanDbEntity;
import com.fenghuajueli.lib_data.entity.db.MonthPlanDbEntityDao;
import com.fenghuajueli.lib_data.entity.db.WeekPlanDbEntity;
import com.fenghuajueli.lib_data.entity.db.WeekPlanDbEntityDao;
import com.fenghuajueli.libbasecoreui.BaseApplication;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

public class TimeBlockManager {
    private static class TimeBlockManagerSingleton {
        private static final com.fenghuajueli.libbasecoreui.manager.TimeBlockManager INSTANCE = new com.fenghuajueli.libbasecoreui.manager.TimeBlockManager();

    }

    public static com.fenghuajueli.libbasecoreui.manager.TimeBlockManager getInstance() {
        return TimeBlockManagerSingleton.INSTANCE;
    }

    private TimeBlockManager() {
    }

    public void saveDayPlanDbEntity(DayPlanDbEntity planDbEntity) {
        /*List<DayPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().queryBuilder().where(DayPlanDbEntityDao.Properties.Id.eq(planDbEntity.getId())).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().deleteInTx(list);
        }*/
        BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().insertOrReplace(planDbEntity);
    }

    public List<DayPlanDbEntity> getDayPlanData(int type) {
        List<DayPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession()
                .getDayPlanDbEntityDao().queryBuilder().where(DayPlanDbEntityDao.Properties.Type.eq(type)).list();;
        return list;
    }

    public void updateDayPlan(DayPlanDbEntity planDbEntity) {
        BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().updateInTx(planDbEntity);
    }

    public List<DayPlanDbEntity> deleteDayPlan(long createTime,int type) {
        List<DayPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().queryBuilder().where(DayPlanDbEntityDao.Properties.Create_time.eq(createTime)).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getTimeBlockDaoMaster().newSession().getDayPlanDbEntityDao().deleteInTx(list);
            if (type == 4) {
                deleteWeekPlan(createTime);
            } else if (type == 5) {
                deleteMonthPlan(createTime);
            }
        }
        List<DayPlanDbEntity> data = getDayPlanData(type);
        return data;
    }


    public void saveWeekPlanDbEntity(WeekPlanDbEntity planDbEntity) {
        BaseApplication.getTimeBlockDaoMaster().newSession().getWeekPlanDbEntityDao().insertOrReplace(planDbEntity);
    }

    public List<WeekPlanDbEntity> getWeekPlanData(long createTime) {
        List<WeekPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession()
                .getWeekPlanDbEntityDao().queryBuilder().where(WeekPlanDbEntityDao.Properties.Create_time.eq(createTime)).list();;
        return list;
    }


    public void deleteWeekPlan(long createTime) {
        List<WeekPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession()
                .getWeekPlanDbEntityDao().queryBuilder().where(WeekPlanDbEntityDao.Properties.Create_time.eq(createTime)).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getTimeBlockDaoMaster().newSession()
                    .getWeekPlanDbEntityDao().deleteInTx(list);
        }
    }


    public void saveMonthPlanDbEntity(MonthPlanDbEntity planDbEntity) {
        BaseApplication.getTimeBlockDaoMaster().newSession().getMonthPlanDbEntityDao().insertOrReplace(planDbEntity);
    }

    public List<MonthPlanDbEntity> getMonthPlanData(long createTime) {
        List<MonthPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession()
                .getMonthPlanDbEntityDao().queryBuilder().where(MonthPlanDbEntityDao.Properties.Create_time.eq(createTime)).list();
        return list;
    }


    public void deleteMonthPlan(long createTime) {
        List<MonthPlanDbEntity> list = BaseApplication.getTimeBlockDaoMaster().newSession()
                .getMonthPlanDbEntityDao().queryBuilder().where(MonthPlanDbEntityDao.Properties.Create_time.eq(createTime)).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getTimeBlockDaoMaster().newSession()
                    .getMonthPlanDbEntityDao().deleteInTx(list);
        }
    }
}
